package com.ruoyi.web;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.MyTokenService;
import com.ruoyi.system.domain.BlogDetails;
import com.ruoyi.system.service.IBlogDetailsService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class BlogviewController extends BaseController {
    @Autowired
    private IBlogDetailsService blogDetailsService;
    @Autowired
    private MyTokenService myTokenService;
    @RequestMapping("/blogpage")
    public List<BlogDetails> blogpage(HttpServletRequest request, HttpServletResponse response){
        Integer from = Integer.valueOf(request.getParameter("from"));
        Integer to = Integer.valueOf(request.getParameter("to"));
        Map<String,Integer> map = new HashMap<String,Integer>();
        map.put("from",from);
        map.put("to",to);
        //System.out.println(blogDetailsService.selectBlogAll());
        return blogDetailsService.selectBlogAll();
    }
    @RequestMapping("/manageblogpage")
    public List<BlogDetails> manageblogpage(@RequestParam("token")String token){
        BlogDetails blogDetails=new BlogDetails();
            String loginrule = myTokenService.getloginrole(token);
            Long ruleid = myTokenService.getloginid(token);
            if (loginrule.equals("employeelogin")) {
                loginrule = "seekerlogin";
            }
            blogDetails.setUserRole(loginrule);
            blogDetails.setUserId(ruleid);
            return blogDetailsService.selectBlogDetailsList(blogDetails);
    }
    @RequestMapping("/posteditblog")
    public BlogDetails posteditblog(@RequestParam("token")String token,@RequestParam("id")String id){

        return blogDetailsService.selectBlogDetailsByBlogId(Long.valueOf(id));

    }
    @RequestMapping("/blogshowlist")
    public List<BlogDetails> blogshowlist(HttpServletRequest request, HttpServletResponse response){
        Integer from = Integer.valueOf(request.getParameter("from"));
        Integer to = Integer.valueOf(request.getParameter("to"));
        Map<String,Integer> map = new HashMap<String,Integer>();
        map.put("from",from);
        map.put("to",to);
        //System.out.println(blogDetailsService.selectBlogAllfromto(map));
        return blogDetailsService.selectBlogAllfromto(map);
    }


    @RequestMapping("/blogdetail")
    public BlogDetails blogDetails(@RequestParam("id") String id){
        //更新观看数量
        BlogDetails blogDetails = new BlogDetails();
        blogDetails.setBlogId(Long.valueOf(id));
        Integer views = blogDetailsService.selectBlogDetailsByBlogId(Long.valueOf(id)).getBlogViews();
        views=views+1;
        blogDetails.setBlogViews(views);
        blogDetailsService.updateBlogDetails(blogDetails);
        //System.out.println("更新后的views"+views+"select="+blogDetailsService.selectBlogDetailsByBlogId(Long.valueOf(id)).getBlogViews());
        return blogDetailsService.selectBlogDetailsByBlogId(Long.valueOf(id));
    }
    @RequestMapping("/deleteblog")
    public AjaxResult deleteblog(@RequestParam("id") String id){
        return toAjax(blogDetailsService.deleteBlogDetailsByBlogId(Long.valueOf(id)));
    }
    @RequestMapping("editblog")
    public AjaxResult editblog(@RequestParam("id") String id,@RequestParam("token")String token,@RequestParam("blog")String blog,@RequestParam("blogtitle")String blogtitle,@RequestParam("blogtags")String blogtags){
        BlogDetails blogDetails = new BlogDetails();
        blogDetails.setBlogId(Long.valueOf(id));
        Date date = new Date(System.currentTimeMillis());
        blogDetails.setBlogDate(date);
        blogDetails.setBlogTitle(blogtitle);
        blogDetails.setBlogContent(blog);
        //多个分隔符
        blogDetails.setBlogTag(String.join(",",blogtags.split("[-；,，;\\s]+")));
        return toAjax(blogDetailsService.updateBlogDetails(blogDetails));

    }
    @RequestMapping("/postblog")
    public AjaxResult postblog(@RequestParam("token")String token,@RequestParam("blog")String blog,@RequestParam("blogtitle")String blogtitle,@RequestParam("blogtags")String blogtags){
        BlogDetails blogDetails = new BlogDetails();
        blogDetails.setBlogCommentsNum(0);
        blogDetails.setBlogViews(0);
        blogDetails.setBlogContent(blog);
        //多个分隔符
        blogDetails.setBlogTag(String.join(",",blogtags.split("[-；,，;\\s]+")));
        blogDetails.setBlogTitle(blogtitle);

        Date date = new Date(System.currentTimeMillis());
        blogDetails.setBlogDate(date);
        if(token.equals("notoken")){
            blogDetails.setUserRole(token);
        }
        else {
            String loginrule = myTokenService.getloginrole(token);
            Long ruleid = myTokenService.getloginid(token);
            if (loginrule.equals("employeelogin")) {
                loginrule = "seekerlogin";
            }
            blogDetails.setUserRole(loginrule);
            blogDetails.setUserId(ruleid);
        }
        return toAjax(blogDetailsService.insertBlogDetails(blogDetails));
    }
    @RequestMapping("/blogsearch")
    public List<BlogDetails> blogsearch(@RequestParam("searchkey")String searchkey){
        System.out.println(searchkey);

        String keyarray[] = searchkey.split("[-；,，;\\s]+");
        List<BlogDetails>selectblogs = new ArrayList<>();
        for(int i=0;i<keyarray.length;i++){
            if(keyarray[i].length()>=2){
                Map<String,String> fromsearchkeys = new HashMap<>();
                fromsearchkeys.put("keyword",keyarray[i]);
                selectblogs.addAll(blogDetailsService.selectBlogDetailsByKeyword(fromsearchkeys));
            }
        }
        //去重
        Set<BlogDetails> playerSet = new TreeSet<>(Comparator.comparing(BlogDetails::getBlogId));
        playerSet.addAll(selectblogs);
        selectblogs.clear();
        selectblogs.addAll(playerSet);
        System.out.println(selectblogs);
        return  selectblogs;


    }
}
